const _ = require('underscore');
const { Service } = require('dx-router-admin');
/*****************************/
class BaseService extends Service{
	constructor(ctx) {
		super(ctx);
	}
	init(){
		/******************************************************/
		this.$on('before_insert',async function(data){
			if(this.$dx.auth&&this.$dx.auth.uid){
				data.user_id = this.$dx.auth.uid;
			}
			this.beforeInsert&&(await this.beforeInsert(data));
		});
		/******************************************************/
		this.$on('after_insert',async function(data){
			this.afterInsert&&(await this.afterInsert(data));
		});
		/******************************************************/
		this.$on('before_update',async function(data){
			if(this.$dx.auth&&this.$dx.auth.uid){
				data.editor_id = this.$dx.auth.uid;
			}
			this.beforeUpdate&&(await this.beforeUpdate(data));
		});
		/******************************************************/
		this.$on('after_update',async function(data){
			this.afterUpdate&&(await this.afterUpdate(data));
		});
		/******************************************************/
		this.$on('before_delete',async function(_ids){
			this.beforeDelete&&(await this.beforeDelete({_ids}));
		});
		/******************************************************/
		this.$on('after_delete',async function(_ids){
			this.afterDelete&&(await this.afterDelete({_ids}));
		});
		/******************************************************/
		this.$on('before_write',async function(data){
			if(this.$dx.auth&&this.$dx.auth.uid){
				if(data._id){
					data.editor_id = this.$dx.auth.uid;
				}else{
					data.user_id = this.$dx.auth.uid;
				}
			}
			this.beforeWrite&&(await this.beforeWrite(data));
		});
		/******************************************************/
		this.$on('after_write',async function(data){
			this.afterWrite&&(await this.afterWrite(data));
		});
	}
	async batchUpdateList(list,count=100){
		const list1 = list.slice(0,count);
		await this.batchUpload(list1);
		const other = list.slice(count);
		//console.error('剩余：',other.length);
		if(other.length){
			return await this.batchUpdateList(other,count);
		}else{
			return true;
		}
	}
	batchUpload(list){
		const promiseList = [];
		for(let i=0;i<list.length;i++){
			const _id = list[i]._id;
			delete list[i]._id;
			const res = this.where({_id}).update(list[i]);
			promiseList.push(res);
		}
		return Promise.all(promiseList);
	}
	/*
	公司名称数据清洗
	*/
	cleanCompanyName(name) {
	    // 1. 标准化文本：去除特殊符号和空格
	    //name = name.replace(/[()\-\s（）]/g, '');
	
	    // 2. 去除地域前缀（优先匹配开头的地区词）
	    const locationWords = ["北京", "上海","苏州","深圳", "广州市", "浙江省", "中国", "香港"];
	    for (const loc of locationWords) {
	        if (name.startsWith(loc)) {
	            name = name.replace(loc, ''); // 移除开头的地区词
	            break; // 只处理第一个匹配到的地域词
	        }
	    }
	    // 3. 去除法律形态词（从尾部匹配）
	    const legalTerms = ["有限公司", "有限责任公司", "股份有限公司", "分公司"];
	    for (const term of legalTerms) {
	        if (name.endsWith(term)) {
	            name = name.substring(0, name.length - term.length); // 移除尾部的法律词
	            break; // 只处理第一个匹配到的法律词
	        }
	    }
	    // 4. 去除行业后缀和冗余词（从中间或尾部匹配）
	    // const industryWords = ["科技", "技术", "信息", "集团", "控股", "股份"];
	    // const redundantWords = ["国际", "数字", "实业", "电子"];
	    // const allRemoveWords = industryWords.concat(redundantWords);
	    // for (const word of allRemoveWords) {
	    //     name = name.replace(new RegExp(word, 'g'), ''); // 全局替换
	    // }
	
	    // 返回清洗后的名称，并去除多余空格
	    return name.trim();
	}
	batchClearName(list){
		return list.map(name=>{
			return this.cleanCompanyName(name);
		})
	}
	
}

module.exports = {
	BaseService
}